<template>
    <el-dialog class="nopadding" title="特征频率" v-if="isShow" :visible.sync="isShow" width="1000px" top="8vh" size="mini" :close-on-click-modal="false">
        <el-form style="height: 48px" label-width="100px">
            <el-form-item label="转频："><el-input-number v-model="freCon"></el-input-number></el-form-item>
        </el-form>
        <div style="width: 100%; height: 65vh; overflow-y: auto">
            <el-tree v-if="partList.length !== 0" :data="partList" :props="defaultProps" @node-click="handleNodeClick" highlight-current node-key="id" :expand-on-click-node="false" default-expand-all ref="tree">
                <span class="custom-tree-node" slot-scope="{ node, data }">
                    <div v-if="data.type < 3" style="display: flex; align-items: center">
                        <img style="margin-right: 5px" :src="require(`@/assets/sensor/${data.type}.png`)" />
                        {{ node.label }}
                    </div>
                    <div v-else-if="data.type < 10" style="display: flex; align-items: center">
                        <img style="margin-right: 5px" :src="require(`@/assets/sensor/${data.type}.png`)" />
                        {{ node.label }}(齿轮数{{ data.gearCount }})
                    </div>
                    <div v-else style="display: flex; align-items: center">
                        <div style="width: 200px">{{ data.name }}({{ (data.speed_ratio * freCon).toFixed(2) }}HZ)</div>
                        <span style="margin-right: 15px" v-for="item1 in 5" :key="item1" @click="check(item1, data.id)">
                            <el-checkbox :label="'X' + item1"></el-checkbox>
                        </span>
                    </div>
                </span>
            </el-tree>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="isShow = false">取 消</el-button>
            <el-button type="primary" @click="set">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import { requestApi } from '@/requset/api'
export default {
    data() {
        return {
            isShow: false,
            defaultProps: {
                children: 'children',
                label: 'name',
            },
            partList: [],
            freCon: 83.33,
            checkData: {},
            b: true,
        }
    },
    methods: {
        set() {
            let list = []
            for (let key in this.checkData) {
                this.checkData[key].checkList.map((item) => {
                    list.push({
                        x: this.checkData[key].speed_ratio * item * this.freCon,
                        y: this.checkData[key].name,
                    })
                })
            }

            this.isShow = false
            this.$emit("charfre",list)
        },
        check(data, id) {
            if (this.b) {
                if (this.checkData[id].checkList.includes(data)) {
                    this.checkData[id].checkList = this.checkData[id].checkList.filter((item) => item !== data)
                } else {
                    this.checkData[id].checkList.push(data)
                }
            }
            this.b = !this.b
        },
        open(device_id) {
            requestApi('post', `part/getlist`, { device_id }).then((res) => {
                if (res.data.code == 0) {
                    let id = 1
                    let dg = (data) => {
                        if (data.children.length !== 0) {
                            data.children.map((item) => {
                                dg(item)
                            })
                        }
                        if (data.type === 0) {
                            this.checkData[id] = {
                                speed_ratio: data.speed_ratio,
                                checkList: [],
                                name: '转频',
                            }
                            data.children.push({
                                type: 10,
                                name: '转频',
                                speed_ratio: data.speed_ratio,
                                id: id++,
                            })
                        } else if (data.type === 1) {
                            this.checkData[id] = {
                                speed_ratio: data.speed_ratio * data.bearing.bl_bpfi,
                                checkList: [],
                                name: 'BPFI',
                            }
                            data.children.push({
                                type: 11,
                                name: 'BPFI',
                                speed_ratio: data.speed_ratio * data.bearing.bl_bpfi,
                                id: id++,
                            })
                            this.checkData[id] = {
                                speed_ratio: data.speed_ratio * data.bearing.bl_bpfo,
                                checkList: [],
                                name: 'BPFO',
                            }
                            data.children.push({
                                type: 11,
                                name: 'BPFO',
                                speed_ratio: data.speed_ratio * data.bearing.bl_bpfo,
                                id: id++,
                            })
                            this.checkData[id] = {
                                speed_ratio: data.speed_ratio * data.bearing.bl_bsf,
                                checkList: [],
                                name: 'BSF',
                            }
                            data.children.push({
                                type: 11,
                                name: 'BSF',
                                speed_ratio: data.speed_ratio * data.bearing.bl_bsf,
                                id: id++,
                            })
                            this.checkData[id] = {
                                speed_ratio: data.speed_ratio * data.bearing.bl_ftfi,
                                checkList: [],
                                name: 'FTFI',
                            }
                            data.children.push({
                                type: 11,
                                name: 'FTFI',
                                speed_ratio: data.speed_ratio * data.bearing.bl_ftfi,
                                id: id++,
                            })
                        } else if (data.type === 2) {
                            data.children.map((item, index) => {
                                if (index !== data.children.length - 1) {
                                    this.checkData[id] = {
                                        speed_ratio: item.children[0].speed_ratio * item.children[0].gearCount,
                                        checkList: [],
                                        name: '啮合频率',
                                    }
                                    item.children.push({
                                        type: 12,
                                        name: '啮合频率',
                                        speed_ratio: item.children[0].speed_ratio * item.children[0].gearCount,
                                        id: id++,
                                    })
                                }
                            })
                        }
                        return data
                    }
                    res.data.data.forEach((item) => dg(item))
                    this.partList = res.data.data
                    this.isShow = true
                }
            })
        },
    },
}
</script>

<style lang="less" scoped></style>
